/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.domain.mainpage;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lightkits.common.CommonMethodsUtil;
import com.lightkits.common.db.entity.BdOnhandQuantity;
import com.lightkits.common.db.entity.FndOrganizationBase;
import com.lightkits.common.db.entity.FndOrganizationTl;
import com.lightkits.common.model.CurrentWorkerInfo;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.soa.common.pipeline.session.SessionInfo;
import com.lightkits.framework.soa.hzero.executor.ServiceExecutor;
import com.lightkits.wms.database.dao.WmsItemBaseMapper;
import com.lightkits.wms.database.model.FndItemInfoModel;
import com.lightkits.wms.dto.mainpage.QueryItemRequestType;
import com.lightkits.wms.dto.mainpage.QueryItemResponseType;
import com.lightkits.wms.dto.mainpage.model.ItemInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * QueryItemInfoExecutor
 *
 * @author menxiao.wu@hand-china.com
 * Created on 2019-02-21 17:25
 */
@Component
public class QueryItemInfoExecutor implements
        ServiceExecutor<QueryItemRequestType, QueryItemResponseType> {


    private final WmsItemBaseMapper mapper;

    @Autowired
    public QueryItemInfoExecutor(WmsItemBaseMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public QueryItemResponseType service(QueryItemRequestType request) {
        QueryItemResponseType response = new QueryItemResponseType();
        response.setSuccessful(true);
        if (ObjectUtil.isNullOrEmpty(request.getOrganizationId())) {
            CurrentWorkerInfo currentWorkerInfo = CommonMethodsUtil.getCurrentWorkerInfo(null);
            FndOrganizationBase base = new FndOrganizationBase().selectOne(
                    new EntityWrapper()
                            .eq(FndOrganizationTl.ORGANIZATION_ID, currentWorkerInfo.getDefaultOrganizationId()));
            if (ObjectUtil.isEmpty(base)) {
                response.setSuccessful(false);
                return response;
            }
            request.setOrganizationId(base.getOrganizationId().toString());
        }
        Page<List<ItemInfoDTO>> result = PageHelper.startPage(request.getPageNum(), request.getPageSize(), true);
        //查询限制条件下的物料
        List<FndItemInfoModel> fndItemInfoModels = mapper.fuzzyQueryItemInfo(
                request.getItemCodeOrDescription(), request.getItemCodeOrId(),
                request.getOrganizationId(),
                SessionInfo.tenantId().toString(),
                SessionInfo.languageCode());
        //物料类型转换
        List<ItemInfoDTO> collect = fndItemInfoModels.stream().map(t -> {
                    ItemInfoDTO dto = new ItemInfoDTO();
                    dto.setItemId(t.getItemId());
                    dto.setItemCode(t.getItemCode());
                    dto.setPrimaryUom(t.getPrimaryUom());
                    dto.setPrimaryUomText(t.getPrimaryUomText());
                    dto.setPrimaryUomId(t.getPrimaryUomId());
                    dto.setOrgCode(t.getOrgCode());
                    dto.setOrgName(t.getOrgName());
                    dto.setDescription(t.getDescription());
                    BigDecimal onHandQty = new BigDecimal(0);
                    //现有量查询
                    EntityWrapper<BdOnhandQuantity> entityWrapper = new EntityWrapper<>();
                    if (!ObjectUtil.isNullOrEmpty(request.getLotNum())) {
                        entityWrapper.eq(BdOnhandQuantity.LOT_NUMBER, request.getLotNum());
                    } else {
                        entityWrapper.isNull(BdOnhandQuantity.LOT_NUMBER);
                    }
                    if (!ObjectUtil.isNullOrEmpty(request.getWmAreaId())) {
                        entityWrapper.eq(BdOnhandQuantity.WM_AREA_ID, request.getWmAreaId());
                    } else {
                        entityWrapper.isNull(BdOnhandQuantity.WM_AREA_ID);
                    }
                    if (!ObjectUtil.isNullOrEmpty(request.getOrganizationId())) {
                        entityWrapper.eq(BdOnhandQuantity.ORGANIZATION_ID, request.getOrganizationId());
                    } else {
                        entityWrapper.isNull(BdOnhandQuantity.ORGANIZATION_ID);
                    }
                    if (!ObjectUtil.isNullOrEmpty(request.getWarehouseId())) {
                        entityWrapper.eq(BdOnhandQuantity.WAREHOUSE_ID, request.getWarehouseId());
                    } else {
                        entityWrapper.isNull(BdOnhandQuantity.WAREHOUSE_ID);
                    }
                    if (!ObjectUtil.isEmpty(t.getItemId())) {
                        entityWrapper.eq(BdOnhandQuantity.ITEM_ID, t.getItemId());
                    } else {
                        entityWrapper.isNull(BdOnhandQuantity.ITEM_ID);
                    }
                    if (!ObjectUtil.isEmpty(t.getPrimaryUom())) {
                        entityWrapper.eq(BdOnhandQuantity.PRIMARY_UOM, t.getPrimaryUom());
                    } else {
                        entityWrapper.isNull(BdOnhandQuantity.PRIMARY_UOM);
                    }
                    onHandQty = new BdOnhandQuantity()
                            .selectList(entityWrapper)
                            .stream()
                            .map(BdOnhandQuantity::getQuantity)
                            .reduce(BigDecimal.ZERO, BigDecimal::add);
                    dto.setOnhandQty(onHandQty.setScale(0, BigDecimal.ROUND_HALF_UP).toString());
                    return dto;
                }
        ).collect(Collectors.toList());
        response.setPageDataCount((int) result.getTotal());
        response.setItems(collect);

        return response;
    }
}
